package com.gin.kotlin.no_gay_alliance.database.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import com.fasterxml.jackson.annotation.JsonIgnore
import com.gin.nga.bbscode.BbsCodeNode
import com.gin.nga.response.reply.ReplyInfo
import com.gin.nga.utils.DiceUtils
import java.time.ZonedDateTime

/**
 * 回复历史，可根据部分条件查询：主题ID,发布时间范围，是否含有图片，是否为匿名，用户ID，用户名关键字，正文和标题关键字
 * @property uuid 唯一ID，由回复ID+发布时间（最后编辑时间）生成
 * @property timestamp 本条创建或更新时间
 * @property replyId 回复ID
 * @property topicId 主题ID
 * @property content 正文
 * @property floorNumber 楼层号
 * @property authorUid 作者用户ID
 * @property username 作者用户名
 * @property title 标题
 * @property topicTitle 主题标题
 * @property anonymous 是否为匿名回复
 * @property hasImage 正文是否含有图片
 * @property postDateTime 回复发布时间
 * @property lastEdit 回复最后编辑时间
 * @property count 统计字段
 * @constructor
 */
@Entity(
    indices = [
        // 搜索用索引
        Index(name = "search_0", value = ["topicId", "postDateTime", "hasImage", "anonymous", "authorUid"]),
        Index(name = "search_1", value = ["postDateTime", "hasImage", "anonymous", "authorUid"]),
        Index(name = "search_2", value = ["hasImage", "anonymous", "authorUid"]),
    ],
)
data class ReplyHistory(
    @PrimaryKey
    var uuid: String,
    var timestamp: ZonedDateTime,
    @ColumnInfo(index = true)
    var replyId: Long,
    @ColumnInfo(index = true)
    var topicId: Long,
    var topicTitle: String? = null,
    var content: String,
    var floorNumber: Int,
    @ColumnInfo(index = true)
    var authorUid: Long,
    var username: String,
    var title: String? = null,
    @ColumnInfo(index = true)
    var anonymous: Boolean = false,
    @ColumnInfo(index = true)
    var hasImage: Boolean = false,
    @ColumnInfo(index = true)
    var postDateTime: ZonedDateTime,
    var lastEdit: ZonedDateTime,
) {
    @Ignore
    var count: Int = 0

    constructor(replyInfo: ReplyInfo) : this(
        uuid = "",
        timestamp = ZonedDateTime.now(),
        topicId = replyInfo.topicId!!,
        replyId = replyInfo.replyId!!,
        content = replyInfo.content!!,
        floorNumber = replyInfo.floorNumber ?: 0,
        authorUid = replyInfo.authorUid?.toLongOrNull() ?: 0,
        username = replyInfo.anonymousName ?: replyInfo.userInfo?.username ?: "",
        title = replyInfo.title,
        anonymous = replyInfo.isAnonymous(),
        postDateTime = replyInfo.postDatetime!!,
        lastEdit = replyInfo.alterInfo?.lastEdit?.timestamp ?: replyInfo.postDatetime!!,
        hasImage = replyInfo.content?.contains("[img]") == true,
    ) {
        this.uuid = "${this.replyId}_${this.lastEdit.toEpochSecond()}"
    }

    /**
     * 解析得到正文节点，用于渲染
     * @return 正文节点
     */
    @JsonIgnore
    fun getBbsCodeNode(): List<BbsCodeNode>? {
        val nodes = BbsCodeNode.parseContent(content)
        //计算随机数
        if (content.contains("[dice]")) DiceUtils.calculate(nodes, authorUid + topicId + replyId)
        return nodes
    }
}

